﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Core;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Forum;
using ZHDJ.WebPage;
using ZHDJ.Core.Caching;
using CacheKeys = ZHDJ.Core.Caching.CacheKeys;

namespace ZHDJ.Web.Portal.Forum
{
    public partial class BBSIndex : PortalPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckLogin(true);
            if (CurrentUser.runSate!="0")
            {
                RedirectTo("default", "~/default.aspx");
            }

            if (!this.IsPostBack)
            {
                this.rptBigForumType.ItemDataBound += RptBigForumType_ItemDataBound;
                LoadData();
            }
        }

        public string GetImageUrl(string imageId)
        {
            string resultUrl = "../../Resource/themes/Default/Images/party/Party-Emblem.jpg";
            if (!string.IsNullOrEmpty(imageId))
            {
                resultUrl = UpFileBusiness.GetFileUrl(imageId);
            }
            return resultUrl;
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        protected void LoadData()
        {
            //聚焦
            this.rptToDayTop.DataSource = PartyForum.GetGridData(0, 5, "", "", SysEnum.ForumOrderby.Focus,false);
            this.rptToDayTop.DataBind();
            //热帖
            this.rptHot.DataSource = PartyForum.GetGridData(0, 5, "", "", SysEnum.ForumOrderby.TodayVisitTimes);
            this.rptHot.DataBind();
            //新帖速递
            this.rpNew.DataSource = PartyForum.GetGridData(0, 5, "");
            this.rpNew.DataBind();

            //主版块
            //this.rptBigForumType.DataSource = MemoryCache.Get<DataSet>(CacheKeys.Cache_Forum_BigForumType, () => { return PartyForum.GetBigForumType(); });
            this.rptBigForumType.DataSource= PartyForum.GetForumType();
            this.rptBigForumType.DataBind();
            //CheckUserInfo();

        }

        public void CheckUserInfo()
        {
            var users = DB.PartyForumUserInfos.Count(a => a.LoginID == CurrentUser.LoginID);

            if (users >= 1) return;
            DB.PartyForumUserInfos.Add(new PartyForumUserInfo()
            {
                ID = StringHelper.NewID,
                BrowseNum = 0,
                EssenceNum = 0,
                ImageID = "",
                Level = 0,
                LoginID = CurrentUser.LoginID,
                NickName = "",
                PostNum = 0,
                ReplyCount = 0,
                Score = 0
            });
            DB.SaveChanges();
        }

        /// <summary>
        /// 绑定子版块
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RptBigForumType_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var dataList = e.Item.FindControl("dltForumInfo") as DataList;
            if (dataList == null)
            {
                return;
            }
            var row = e.Item.DataItem as DataRowView;
            var id = Convert.ToString(row["ID"]);
            dataList.DataSource = PartyForum.GetForumType(id);
            dataList.DataBind();
        }

      

    }
}